package org.example.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * Utility class for text processing tasks.
 */
public class TextUtils {

    /**
     * Processes a text file and returns its content as a single string.
     * All non-letter characters are replaced with spaces, and all letters are converted to lowercase.
     *
     * @param filePath the path of the text file to process.
     * @return the processed text content.
     */
    public String processTextFile(String filePath) {
        StringBuilder result = new StringBuilder();

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                // Replace non-letter characters with space and convert to lowercase
                line = line.replaceAll("[^a-zA-Z]+", " ").toLowerCase().trim();
                result.append(line).append(" "); // Append each line to the result with a space separator
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result.toString().trim(); // Return the result and trim any trailing spaces
    }
}
